class Node {
    constructor(data) {
        this.prev = null;
        this.data = data;
        this.next = null;
    }
}

class LxDoubleLinkedList {
    constructor() {
        this.head = null;
        this.tail = null;
        this.size = 0;
    }

    append(data) {
        let node = new Node(data);
        if (this.size === 0) {
            this.head = node;
            this.tail = node;
        } else {
            node.prev = this.tail;
            this.tail.next = node;
            this.tail = node;
        }
        this.size++;
    }

    insert(index, data) {
        if (index < 0 || index > this.size) return false;
        var node = new Node(data);
        if (index === 0) {
            this.head.prev = node;
            node.next = this.head;
            this.head = node;
            this.size++;
        } else if (index === this.size) {
            this.append(data);
        } else {
            let current = this.head;
            for (var i = 0; i < index; i++) {
                current = current.next;
            }
            console.log(current);
            node.prev = current.prev;
            node.next = current;
            current.prev.next = node;
            current.prev = node;
            this.size++;
        }
    }

    get(index) {
        if (index < 0 || index >= this.size) return false;
        let current = this.head;
        for (var i = 0; i < index; i++) {
            current = current.next;
        }
        return current.data;
    }

    indexOf(data) {
        let current = this.head;
        let index;
        for (var i = 0; i < this.size - 1; i++) {
            if (data === current.data) {
                index = i;
                break;
            }
            current = current.next;
        }
        return typeof index === "number" ? index : -1;
    }

    update(index, data) {
        if (index < 0 || index >= this.size) return false;
        let current = this.head;
        for (var i = 0; i < index; i++) {
            current = current.next;
        }
        current.data = data;
    }

    removeAt(index) {
        if (index < 0 || index >= this.size) return false;
        let current = this.head;
        if (index === 0) {
            this.head.next.prev = null;
            this.head = this.head.next;
        } else if (index === this.size - 1) {
            this.tail.prev.next = null;
            this.tail = this.tail.prev;
        } else {
            let current = this.head;
            for (var i = 0; i < index; i++) {
                current = current.next;
            }
            current.prev.next = current.next;
            current.next.prev = current.prev;
        }
        this.size--;
    }

    toString(tag = " ") {
        if (!this.size) return "";
        var current = this.head;
        var str = "";
        while (current) {
            str += current.data + tag;
            current = current.next;
        }
        return str;
    }

    forwardString() {
        return this.toString(",");
    }

    backwordString() {
        if (!this.size) return "";
        var current = this.tail;
        var str = "";
        while (current) {
            str += current.data + tag;
            current = current.prev;
        }
        return str;
    }
}

let doubleLinkedList = new LxDoubleLinkedList();
doubleLinkedList.append(1);
doubleLinkedList.append(2);
doubleLinkedList.insert(2, 111);

// console.log(doubleLinkedList.get(2));
// console.log(doubleLinkedList.indexOf(3));
// console.log(doubleLinkedList.update(2, 1111));
doubleLinkedList.removeAt(1);
console.log(doubleLinkedList.toString());

console.log(doubleLinkedList);
